<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorate="~{admin/layout}">

<head>
	<title>Coach Manage</title>
	<style>

	</style>
</head>

<body layout:fragment="content">
	<div class="card shadow">
		<div class="card-header border-bottom d-flex justify-content-between align-items-center">
			<h5 class="card-title mb-0">Coach List</h5>
			<div class="d-grid gap-2 d-md-block">
				<button class="btn btn-sm btn-primary" type="button" onclick="modifyCoachModal('')">New</button>
			</div>
		</div>
		<div class="card-body">
			<table id="coachTable"></table>
		</div>
		<!--<div class="card-footer pt-0">
			Footer
		</div>-->
	</div>
	<!---->
	<div class="modal fade" id="coachModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
		aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">Coach Setting</h4>
				</div>
				<div class="modal-body">
					<div class="row">
						<div class="col-md-12">
							<form id="coachForm">
								<input type="hidden" class="form-control" value="" id="coachId" name="coachId">
								<div class="mb-3">
									<label class="form-label">Full-Name</label>
									<input type="text" class="form-control" value="" id="coachName" name="coachName">
								</div>
								<div class="mb-3">
									<label class="form-label">Mobile</label>
									<input type="text" class="form-control" value="" id="coachMobile" name="coachMobile">
								</div>
								<div class="mb-3">
									<label class="form-label">Avatar</label>
									<input type="hidden" class="form-control" value="" id="coachAvatar" name="coachAvatar">
									<div class="quickgo-file-main">
										<img src="" alt="">
										<div>
											<button>
												<span>Select Picture</span>
												<input type="file" onchange="MyUpload(this, 'coachAvatar');">
											</button>
										</div>
									</div>
								</div>
								<!--<div class="mb-3">
									<label class="form-label">Job-Status</label>
									<select class="form-control" value="" id="coachStatus" name="coachStatus">
										<option value="0">Depart</option>
										<option value="1">Job</option>
									</select>
								</div>-->
								<!-- Save button -->
								<div class="d-flex justify-content-end mt-4">
									<button type="button" class="btn btn-default"
										onclick="closeCoachModal()">Close</button>
									<button type="submit" class="btn btn-primary">Submit</button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal -->
	</div>
	<script layout:fragment="script">
		var coachFormValidate = null
		$(document).ready(function () {
			coachFormValidate = 
			$("#coachForm").validate({
				rules: {
					coachId: {
						required: false
					},
					coachName: {
						required: true
					},
					coachMobile: {
						required: true
					},
					coachAvatar: {
						required: true
					},
				},
				submitHandler: function (form) {
					var formData = $(form).serialize();
					if(!$('#coachAvatar').val()){
						layer.msg('Select a avatar');
						return false
					}
					saveCoach(formData);
					return false
				}
			})
			loadCoachTable()
		});
		function loadCoachTable() {
			$('#coachTable').bootstrapTable('destroy');
			$('#coachTable').bootstrapTable({
				url: "[[@{'/admin/coachs'}]]",
				uniqueId: 'coachId',
				dataType: "json",
				pagination: true,
				pageSize: 10,
				queryParams: function (params) {
					var temp = {
						size: params.limit,
						page: (params.offset / params.limit),
						sort: params.sort,
						sortOrder: params.order
					};
					return temp;
				},
				sidePagination: "server",
				pageList: [30, 50],
				columns: [{
					title: 'Sort',
					formatter: function (value, row, index) {
						var pageSize = $('#coachTable').bootstrapTable('getOptions').pageSize;
						var pageNumber = $('#coachTable').bootstrapTable('getOptions').pageNumber;
						return pageSize * (pageNumber - 1) + index + 1;
					}
				}
				, {
					field: 'coachName',
					title: 'Full-Name',
					formatter: function (value, row, index) {
						return value
					}
				}
				, {
					field: 'coachMobile',
					title: 'Mobile',
					formatter: function (value, row, index) {
						return value
					}
				}
				, {
					field: 'coachAvatar',
					title: 'Avatar',
					formatter: function (value, row, index) {
						return '<img class="avatar-img rounded-2 avatar-sm" src=" ' + value + ' " alt="">'
					}
				}
				, {
					field: 'coachTime',
					title: 'Created',
					formatter: function (value, row, index) {
						return value
					}
				}
				, {
					field: 'coachStatus',
					title: 'Job-Status',
					formatter: function (value, row, index) {
						return ['Depart', 'In Job'][value]
					}
				}
				, {
					title: 'Operate',
					field: 'coachId',
					class: 'text-center',
					width: '20%',
					formatter: function (value, row, index) {
						if(row.coachStatus == 0){
							return ''
						}
						let res = '<button class="btn btn-sm btn-warning" onclick="modifyCoachModal(\'' + value + '\')">Modify</button>&nbsp;';
						res += '<button class="btn btn-sm btn-danger " onclick="departCoach(\'' + value + '\')">Depart</button>';
						return res;
					}
				},],
				onPostBody: function (value) {
					$(".fixed-table-container").css('padding-bottom', '37px');
				}
			});
		}
		function closeCoachModal() {
			coachFormValidate.resetForm()
			$('#coachModal').modal('hide')
		}
		function modifyCoachModal(coachId) {
			$(".quickgo-file-main").children("img").css("display", 'none')
			if (coachId) {
				var data = $("#coachTable").bootstrapTable('getRowByUniqueId', coachId);
				$('#coachId').val(data.coachId)
				$('#coachName').val(data.coachName)
				$('#coachMobile').val(data.coachMobile)
				$('#coachAvatar').val(data.coachAvatar)
				if(data.coachAvatar){
					$(".quickgo-file-main").children("img").attr("src", data.coachAvatar).css("display", 'block')
				}
			} else {
				$('#coachId').val('')
				$('#coachName').val('')
				$('#coachMobile').val('')
				$('#coachAvatar').val('')
			}
			$('#coachModal').modal('show')
		}
		function saveCoach(data) {
			$.post(ctx + "/admin/updateCoach", data, function (resp) {
				if (resp.status == 0) {
					loadCoachTable();
					$('#coachModal').modal('hide');
				}
				layer.msg(resp.msg);
			})
		}
		function departCoach(coachId) {
			layer.msg("Do you want to depart coach", {
				title: 'tip', icon: 3, btn: ["Confirm", "Cancel"],
				yes: function () {
					$.ajax({
						type: "get",
						url: ctx + "/admin/departCoach",
						data: {
							"coachId": coachId,
						},
						success: function (resp) {
							if (resp.status == 0) {
								loadCoachTable()
							}
							layer.msg(resp.msg);
						}
					});
				},
				btn2: function () {
					// layer.msg("Cancel");
				}
			});
		}
	</script>
</body>

</html>